import os
import fmt
import libc_temp
import syscall

var mask_ptr = new sigset_t // ptr<sigset_t>

 // 注册所有信号
if sigfillset(mask_ptr) == -1 {
    throw libc_strerror()
}

// #define	SIG_BLOCK     0		 /* Block signals.  */
// #define	SIG_UNBLOCK   1		 /* Unblock signals.  */
// #define	SIG_SETMASK   2		 /* Set the set of blocked signals.  */
if (sigprocmask(0, mask_ptr, null) == -1) {
    throw libc_strerror()
}

var sigfd = signalfd(-1, mask_ptr, 2048)
if sigfd == -1 {
    throw libc_strerror()
}

var sig_info = signalfd_siginfo_t{}

for int i = 0; i < 100; i+=1 {
    var (len, err) = try syscall.read(sigfd as int, sig_info as cptr, sizeof(signalfd_siginfo_t))
    if err.has {
       if err.msg == 'Resource temporarily unavailable' {
           fmt.printf('not received any sig, will sleep\n')
       } else {
           throw fmt.sprintf('read sigfd %d err=%s', sigfd, err.msg)
       }
    } else {
       fmt.printf('read sig %d success\n', sig_info.ssi_signo)
       break
    }

    usleep(2000 as u32 * 1000)
}